package DCRS::Customer;
#----------------------------------------------------------------------------
# Name:         Customer.pm
# Purpose:      Process DCRS Clarion database files for customer information
# 
# Notes:        This module is part of a larger DCRS:: namespace 
#
# Author:       Kingsley Wright
#
# Created:      2007-2008
# Copyright:    (c) 2008 by DCRS Solutions, Inc.
# Licence:      TBD - All rights reversed, DCRS Solutions, Inc.
#----------------------------------------------------------------------------
use strict;
use warnings;
use CGI::Lite;
use base 'CGI::Application';
use Clarion;
use Tie::File::AsHash;
use CGI::Carp::DebugScreen (
    debug       => 1,
    engine      => 'HTML::Template',
    lines       => 5,
    modules     => 1,
    environment => 1,
    overload    => 1,
    raw_error   => 0,
);

# Path information/Global Variables
my $DBPATH    = '/media/officedb/PUBLIC/OFFICE4/';
my $cgipath   = '/cgi-bin/';
my $localtime = localtime();
my @cust_loop = ();
my @main_loop = ();

sub setup {
    my $self = shift;
    $self->start_mode('list');
    $self->run_modes(
        'all'   => 'show_customer_list',
        'detail' => 'show_customer_list',
        'report' => 'start_report',
    );
}

sub start_report {
  my $self = shift;
  my $report_id = $self->param('report_id') || 'expd';

  if ( $report_id eq 'expd' ) { 
    my $title = "Sites Currently Expired";
    my $template     = $self->load_tmpl(
        'customer/customer_list.tmpl',
        global_vars       => 1,
        loop_context_vars => 1
    );
  
  }
}

sub show_customer_list {
    my $self         = shift;
    my $q            = $self->query();
    my $page_title   = 'DCRS Customer Information';
    my $script_title = "DCRS Customer Information";
    my $template     = $self->load_tmpl(
        'customer/customer_list.tmpl',
        global_vars       => 1,
        loop_context_vars => 1
    );
    
    get_customer_list($self);
    make_main_loop($self);

    $template->param(
        startform     => $q->start_form(),
        endform       => $q->end_form(),
        main_loop     => \@main_loop,
        page_title    => $page_title,
        script_title  => $script_title,
        localtime     => $localtime
    );

    return $template->output;
}

sub make_main_loop {
    my $self = shift;
    
    # Available but unused slices of the customer record:
    #--------------------------------------------------------------------------
    # @$cust_ref[11]; #LORDER, last order
    # @$cust_ref[12]; #LCALL, last call
    # @$cust_ref[13]; #P1_FNAME <-- these are coming out unreadable from the db
    # @$cust_ref[14]; #P2_FNAME <-- same for these - need scrubbing
    #--------------------------------------------------------------------------
        
    for my $cust_ref ( sort { $a->[3] cmp $b->[3] } @cust_loop ) {
                  
        my $cust_name = @$cust_ref[3];
        
        my $cust_comment;
        if (@$cust_ref[15]) {
            $cust_comment = @$cust_ref[15];
        }
        else {
            $cust_comment = 'no notes';
        }
                
        # Title Tags for mouseovers 
        my $site_title_tag =
          "Click to see details for this site";    
        my $addr_title_tag =
          "Click for directions to this site from DCRS"; 
        my $google_title_tag = 
          'http://maps.google.com/maps?f=d&hl=en&geocode=&saddr=2605+Metro,+63043&daddr='
          . url_encode( @$cust_ref[4] ) . ','
          . url_encode( substr( @$cust_ref[8], 0, 5 ) );

        # Format the phone number string
        my $cust_phone;
        if ( !@$cust_ref[9] ) {
            $cust_phone = "&nbsp;";
        }
        else {
            $cust_phone = '('
            . substr( @$cust_ref[9], 0, 3 )
            . ")&nbsp;"
            . substr( @$cust_ref[9], 3, 3 )
            . '-'
            . substr( @$cust_ref[9], 4, 4 );
        }

        # Format the address string
        my $cust_address;
        if ( !@$cust_ref[4] ) {
            $cust_address = 'No address in record';
        }
        elsif ( @$cust_ref[5] ) {
            $cust_address  = @$cust_ref[4] . '<br>';              #Street 
            $cust_address .= @$cust_ref[5] . '<br>';              #Street 2
            $cust_address .= @$cust_ref[6] . '&nbsp;';            #City
            $cust_address .= @$cust_ref[7] . '&nbsp;';            #State
            $cust_address .= substr( @$cust_ref[8], 0, 5 );       #Zip
        }
        else {
            $cust_address =  @$cust_ref[4] . '<br>';               #Street
            $cust_address .= @$cust_ref[6] . "&nbsp;";            #City
            $cust_address .= @$cust_ref[7] . "&nbsp;";            #State
            $cust_address .= substr( @$cust_ref[8], 0, 5 );       #Zip
        }

        # Join the main and sub account numbers with a "-"for display
        my $cust_id = join( '-', @$cust_ref[1], @$cust_ref[2] );

        # Join the name with the cust_id to create a search object
        my $site_id = url_encode(
            join( ":", @$cust_ref[1], @$cust_ref[2], @$cust_ref[3] ) );

        # Determine the Customer Status
        my $support_status;
        if ( @$cust_ref[10] ) {
            $support_status = 'MAJOR ACCOUNT';
        }
        else {
            $support_status = '&nbsp;';
        }
        
        my %row = (
          cust_id       => $cust_id,
          cust_name     => $cust_name,
          cust_address  => $cust_address,
          cust_phone    => $cust_phone,
          cust_comment  => $cust_comment
        );

        push( @main_loop, \%row );
    }
}

sub get_customer_list {
    my $self = shift;
    my $q = $self->query();
    my $search = uc($q->param('search_text'));

    my $dbh = new Clarion $DBPATH. 'custmr.dat';
    for ( 1 .. $dbh->last_record ) {
        my @r = $dbh->get_record(
            $_,          'NUMBER',   'SUB',        'NAME',
            'ADDR1',     'ADDR2',    'CITY',       'STATE',
            'ZIP',       'PHONE',    'MAJOR_ACNT', 'LAST_ORD',
            'LAST_CALL', 'P1_FNAME', 'P2_LNAME',   'COMMENTS'
        );
        next if $r[0];
        next if $q->param('search_text') and !($r[3] =~ m/$search/);
        push @cust_loop, [@r];
    }

    $dbh->close();
}
1;
